import { ref } from 'vue'
import {userAdd,userEdit,userStatus} from '@/api/user';
import useInstance from '@/hooks/useInstance';
export const useUser = (getUserList) => {
  const {global}  = useInstance()
  const userAddAndEditRef = ref()
  const assignRoleRef = ref()
  const addBtn = ()=>{
    console.log('----------用户新增-------',userAddAndEditRef.value);
    userAddAndEditRef.value?.show('add')
  }
  const editBtn = (index,row)=>{
    userAddAndEditRef.value?.show('edit',row)
  }
  const deleteBtn = (row)=>{}

  const save = async(params)=>{
    console.log('--------------------------');
    let res;
   if(params.action == 'add'){
    res  = await userAdd(params)
   }
   if(params.action == 'edit'){
    res  = await userEdit(params)
   }
   if(res && res.code == 200){
      getUserList();
      global.$message({ message: res.msg, type: "success" })
   }
  }

  // 修改状态
  const changeStaus =  async(index,rows)=>{
    console.log('-----------changeStaus---------------',rows);
    let res = await userStatus(rows.accountId)
    if(res && res.code == 200){
      getUserList();
      global.$message({ message: res.msg, type: "success" })
   }
  }
  const assignRole = (index,row)=>{
    assignRoleRef.value?.show(row)
  }
  return {
    userAddAndEditRef,
    addBtn,
    editBtn,
    deleteBtn,
    save,
    changeStaus,
    assignRole,
    assignRoleRef
  }
}